
//
//  RootTabBarViewController.m
//  daotianjinfu
//
//  Created by hzk on 23/5/8.
//  Copyright © 2023年 hzk. All rights reserved.
//

#import "HKRootTabBarViewController.h"
#import "HKCommonDefine.h"
#import "HKUser.h"
#import "HKRequestDefine.h"
#import <WZLBadge/WZLBadgeImport.h>

@interface HKRootTabBarViewController ()<UINavigationControllerDelegate, UITabBarControllerDelegate>

@end

@implementation HKRootTabBarViewController


#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self addViewSubController];
    self.tabBar.backgroundColor = [UIColor whiteColor];
}

#pragma mark

-(void)addViewSubController{
    
    NSArray *controllerArr = @[@"HKHomePageViewController",
                                @"HKNoticeViewController",
                                @"HKProfileViewController"];
    NSArray * titleArr = @[@"首页",@"消息",@"我的"];
    
    NSArray * imageArr = @[@"House",
                           @"ChatCircle",
                           @"SmileyBlank"];
    NSArray * selectImageArr = @[@"House_selected",
                                 @"ChatCircle_selected",
                                 @"SmileyBlank_selected"];
    
    NSMutableArray * nvArr = [[NSMutableArray alloc]init];
    
    [controllerArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        Class cl = NSClassFromString(obj);
        UIViewController * viewController = [[cl alloc]init];
        
        UIImage * image = [[UIImage imageNamed:imageArr[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        UIImage * selectImage = [[UIImage imageNamed:selectImageArr[idx]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        viewController.tabBarItem = [[UITabBarItem alloc]initWithTitle:titleArr[idx] image:image selectedImage:selectImage];
        
        [viewController.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -5)];
        [nvArr addObject:viewController];
    }];
    
    self.viewControllers = nvArr;
}

-(void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers{
    [super setViewControllers:viewControllers];
    for (id obj in viewControllers) {
        if ([obj isKindOfClass:[UINavigationController class]]) {
            UINavigationController* naviCtrl = obj;
            naviCtrl.delegate = self;
        }
    }
}

- (void)showMessageTabbarItemBadge{
    UITabBarItem *item = self.tabBar.items[1];
    //it is necessary to adjust badge position
    item.badgeCenterOffset = CGPointMake(0, 5);
    [item showBadgeWithStyle:WBadgeStyleRedDot value:0 animationType:WBadgeAnimTypeNone];
}

- (void)clearMessageTabbarItemBadge{
    UITabBarItem *item = self.tabBar.items[1];
    [item clearBadge];
}

@end
